package beanstao.util.module;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.dom4j.Element;

import beanstao.util.collections.ClassMap;
import beanstao.util.interfaces.IManager;
import beanstao.util.services.IService;
import beanstao.util.setting.Setting;

public class ModuleStartSetting extends Setting<Element>
{

	/**
	 * 本类型的单建实例
	 * 
	 * @return 返回本类型的单建实例(延迟加载,并对多线程进行处理)
	 */
	public static ModuleStartSetting ME()
	{
		return Single.instance;
	}

	/**
	 * 为单建模式设计的内部静态类型
	 */
	static class Single
	{
		/**
		 * 多线程锁
		 */
		static Object lock = new Object();
		static
		{
			synchronized (lock)
			{
				instance = new ModuleStartSetting();
			}
		}
		static ModuleStartSetting instance;
	}

	/**
	 * 针对单建模式,私有化本类型构造函数
	 */
	private ModuleStartSetting()
	{
	}

	private static final HashMap<String, ClassMap<?>> _moduleMap = new HashMap<String, ClassMap<?>>();

	public HashMap<String, ClassMap<?>> getModuleMap()
	{
		return _moduleMap;
	}

	private static final ArrayList<Class<IService>> _doorList = new ArrayList<Class<IService>>();

	public ArrayList<Class<IService>> getDoorList()
	{
		return _doorList;
	}

	@SuppressWarnings("unchecked")
	@Override
	protected void fillSettingData(Element element)
	{
		List<Element> modultEleList = element.elements();
		for (Element moduleEle : modultEleList)
		{

			List<Element> classEleList = moduleEle.elements();
			if ("door".equals(moduleEle.getName()))
				for (Element classEle : classEleList)
					try
					{
						_doorList.add((Class<IService>) Class.forName(classEle.getTextTrim()));
					}
					catch (Exception e)
					{
						this.logWarn("Door类型未找到。" + classEle.getTextTrim(), e);
					}
			else if ("module".equals(moduleEle.getName()))
			{
				Class<IManager> navigation = null;
				Class<IManager> presenter = null;
				Class<IManager> dal = null;
				Class<IManager> cache = null;

				for (Element classEle : classEleList)
				{
					String localname = classEle.getName().toLowerCase();
					try
					{
						if ("navigation" == localname)
							navigation = (Class<IManager>) Class.forName(classEle.getTextTrim());
						else if ("presenter" == localname)
							presenter = (Class<IManager>) Class.forName(classEle.getTextTrim());
						else if ("dal" == localname)
							dal = (Class<IManager>) Class.forName(classEle.getTextTrim());
						else if ("cache" == localname)
							cache = (Class<IManager>) Class.forName(classEle.getTextTrim());
						else
							this.logWarn("在 ModuleStartSetting 中有未知的类型：" + localname);
					}
					catch (ClassNotFoundException e)
					{
						this.logWarn(localname + " 类型未找到。" + classEle.getTextTrim(), e);
					}
				}

				ClassMap<IManager> moduleClassMap = new ClassMap<IManager>();
				moduleClassMap.put("navigation", navigation);
				moduleClassMap.put("presenter", presenter);
				moduleClassMap.put("dal", dal);
				moduleClassMap.put("cache", cache);
				_moduleMap.put(moduleEle.attributeValue("name"), moduleClassMap);
			}

		}
	}

}
